#include "Scene.h"
#include "Block.h"
#include "Player.h"
#include <vector>

Scene::Scene()
{
	//kleurtjes defineren
	Color yellow = Color(1,1,0);
	Color red = Color(1,0,0);
	Color blue = Color(0,0,1);
	Color orange = Color(1,1,0);
	Color green = Color(0,1,0);
	Color white = Color(1,1,1);
	Color black = Color(0,0,0);
	Color purple = Color(1,0,1);

	//wereld aanmaken
	m_worldEntity = new EntityGroup();
	m_worldEntity->setName("WorldEntity");

	FILE* file = fopen( "world.txt", "r" );
	if ( file != NULL ) {

		char* line = new char[255];
		char* type = new char[10];
		std::vector<AbstractEntity *> groups ;
		groups.push_back(m_worldEntity);
	
		while ( fgets( line, 255, file ) ) { // read every line
			// find type
			sscanf( line, "%10s", type );
			readLine( type, line, groups );
		}
		fclose( file );
		delete line;
		delete type;
	}

	//player aanmaken
	//m_player = new Player( Position(0,0,0) );
	//m_worldEntity->add( m_player );

	//level aanmaken
	//EntityGroup* level = new EntityGroup();
	//level->setName("Level");

//	Block *b1 = new Block(Position(0,-25,0), Dimensions(50,5,50), red);
//	level->add(b1);

//	Block *b2 = new Block(Position(5,-25,50), Dimensions(50,5,50), yellow);
//	level->add(b2);

//	Block *b3 = new Block(Position(-50,-25,-10), Dimensions(50,5,50), purple);
//	level->add(b3);

//	Block *b4 = new Block(Position(60,-25,50), Dimensions(50,5,50), blue);
//	level->add(b4);

//	Block *b5 = new Block(Position(0,-30,0), Dimensions(500,5,500), green);
//	level->add(b5);

	//m_worldEntity->add(level);
}

void Scene::update( float frametime )
{
	std::vector<Matrix4> matrixes;
	m_worldEntity->preFrame(frametime, matrixes);
	m_worldEntity->frame(frametime);
	m_worldEntity->postFrame();

	std::vector<AbstractEntity*>::const_iterator it, it2;
	for( it = m_worldEntity->getChildren().begin(); it != m_worldEntity->getChildren().end(); it++ )
	{
		//for( it2 = m_worldEntity->getChildren().begin(); it2 != m_worldEntity->getChildren().end(); it2++ )
		//{
		//	(*it)->checkCollision( *it2 );
		//}
		if(m_player != (*it))
			//m_player->checkCollision(*it);
		(*it)->checkCollision(m_player );
	}
}

void Scene::readLine( char* type, char* line, std::vector<AbstractEntity*> &groups ) {
	float posX, posY, posZ, dimX, dimY, dimZ;
	
	if(strcmp(type, "block") == 0){
		char* color = new char[10];
		Color realcolor;
		Color yellow = Color(1,1,0);
		Color red = Color(1,0,0);
		Color blue = Color(0,0,1);
		Color orange = Color(1,1,0);
		Color green = Color(0,1,0);
		Color white = Color(1,1,1);
		Color black = Color(0,0,0);
		Color purple = Color(1,0,1);

		sscanf( line, "%10s %f,%f,%f %f,%f,%f %10s", type, &posX, &posY, &posZ, &dimX, &dimY, &dimZ, color );
		if(strcmp(color, "purple") == 0)
			realcolor = purple;
		if(strcmp(color, "green") == 0)
			realcolor = green;
		if(strcmp(color, "red") == 0)
			realcolor = red;
		if(strcmp(color, "blue") == 0)
			realcolor = blue;
		if(strcmp(color, "orange") == 0)
			realcolor = orange;
		if(strcmp(color, "yellow") == 0)
			realcolor = yellow;
		if(strcmp(color, "white") == 0)
			realcolor = white;
		if(strcmp(color, "black") == 0)
			realcolor = black;
		AbstractEntity * temp = m_entfactory.createEntity("Block");
		Block * temp2 = dynamic_cast<Block*> (temp);
		temp2->setPosition(Position(posX, posY, posZ));
		temp2->setColor(realcolor);
		temp2->setSize(Dimensions(dimX, dimY, dimZ));
		groups.back()->add(temp2);
	}
	if(strcmp(type, "player") == 0){
		sscanf( line, "%10s %f,%f,%f", type, &posX, &posY, &posZ );
		m_player = dynamic_cast<Player *> (m_entfactory.createEntity("Player"));
		m_player->setPosition(Position(posX, posY, posZ));
		//m_player->setRotationVelocity(Vector(0,50,50));
		groups.back()->add(m_player);
	}
	if(strcmp(type, "level") == 0){
		//add new entity group
		char* name = new char[10];
		sscanf( line, "%10s %10s", type, name );
		EntityGroup* temp = new EntityGroup();
		temp->setName(name);
		
		groups.push_back(temp);
		delete name;
	}
	if(strcmp(type, "endlev") == 0){
		//add end entity group
		AbstractEntity* temp = groups.back();
		groups.pop_back();
		groups.back()->add(temp);
	}
	return;	
}